
$(function() {

	$("input[id^='material_s']:visible").autocomplete(Test.material);
	
	Tip.emp('退货人');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("退货人：");
		} else if (a == 'oper') {
			label.html("录入员：");
		} 
	});
	
});

var ReturnedItem = {
	
	popup: function (e, p2, id, material) {
		$.cookie('id_', id);

		$.cookie('status_', status);
		
		p2 = query(p2);
		$.cookie('p2', p2);
		
		var c1 = 'material.php?do=ReturnedItem.edit&id=' + id;
		var c2 = 'material.php?do=ReturnedItem.view&id=' + id;

		var a1 = ['编辑信息', 'url', 'ReturnedItem.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];
		
		var s = c1 + ", " + c2;

		return this.popup_(e, [a1, a2], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'material.php?do=ReturnedItem.ss&id=' + id;
		var bb = ['修改状态', 'url', 'ReturnedItem.dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},
	
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=ReturnedItem.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'material.php?do=ReturnedItem.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=ReturnedItem.audit&item=59', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;

						body.emp = form.emp.value;
						body.date = form.date.value;
						body.mold = form.mold.value;						
						
						body.name = form.name.value;						
						body.standard = form.standard.value;

						body.amount = form.amount.value;
						body.unit = form.unit.value;
						body.price = form.price.value;

						body.content = form.content.value;
						body.status = form.status.value;
						
						body.receive = form.receive.value;
						if (body.receive == '2') {							
							body.receiver = form.supplier.value;
							body.receiver_x = Supplier.supplier_x(form);
						} else {
							body.receiver = form.applicant.value;
						}

						$(this).dialog("close");

						var url = 'material.php?do=ReturnedItem.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	expunge_: function (form) {
		$.getJSON('material.php?do=ReturnedItem.audit&item=52', function(json) {
			if (json.status == 1) {
				expunge(form);
			} else {
				_error();
			}
		});
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=ReturnedItem.delete' + p2;
		var c9 = 'material.php?do=ReturnedItem.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c9;

		return popup(e, [a1, a9], s);
	}
};